//
//  NSDate+HHChat.m
//  HHChatKit
//
//  Created by Henry on 2021/5/10.
//

#import "NSDate+HHChat.h"
#import <HHTool/NSDate+HHTime.h>
#import "HHResources.h"

@implementation NSDate (HHChat)

- (NSString *)shortTimeMessageString {
    return [self shortTimeMessageStringAtWeek:NO];
}

- (NSString *)shortTimeMessageStringAtWeek:(BOOL)week {
    
    NSDate *today = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekday;
    
    NSDateComponents *todayComponents = [calendar components:unit fromDate:today];
    NSDateComponents *myComponents = [calendar components:unit fromDate:self];
    
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc ] init];

    NSString *prefix = @"";
    
    if (todayComponents.year != myComponents.year) {
        dateFmt.dateFormat = @"yy-M-d";
    } else {

        // 当天（月份和日期一致才是）
        if (todayComponents.month == myComponents.month && todayComponents.day == myComponents.day) {
            dateFmt.dateFormat = GetHCLocalizableString(@"TodayFormat");
            dateFmt.AMSymbol = GetHCLocalizableString(@"AM");
            dateFmt.PMSymbol = GetHCLocalizableString(@"PM");
        } else {
            
            // 昨天（以“现在”的时候为基准-1天）
            NSDate *yesterdayDate = [NSDate dateWithTimeInterval:-60*60*24 sinceDate:today];
            NSDateComponents *yesterdayComponents = [calendar components:unit fromDate:yesterdayDate];

            // 前天（以“现在”的时候为基准-2天）
            NSDate *beforeYesterdayDate = [NSDate dateWithTimeInterval:-60*60*24*2 sinceDate:today];
            NSDateComponents *beforeYesterdayComponents = [calendar components:unit fromDate:beforeYesterdayDate];

            if (yesterdayComponents.month == myComponents.month && yesterdayComponents.day == myComponents.day) {
                prefix = [GetHCLocalizableString(@"Yesterday") stringByAppendingString:@" "];
                dateFmt.dateFormat = @"HH:mm";
            } else if (beforeYesterdayComponents.month == myComponents.month && beforeYesterdayComponents.day == myComponents.day) {
                prefix = [GetHCLocalizableString(@"BeforeYesterday") stringByAppendingString:@" "];
                dateFmt.dateFormat = @"HH:mm";
            } else {
                dateFmt.dateFormat = @"M-d";
                if (week) {
                    NSArray *weekdayArray = @[GetHCLocalizableString(@"Sunday"), GetHCLocalizableString(@"Monday"), GetHCLocalizableString(@"Tuesday"), GetHCLocalizableString(@"Wednesday"), GetHCLocalizableString(@"Thursday"), GetHCLocalizableString(@"Friday"), GetHCLocalizableString(@"Saturday")];
                    // 间隔天数,模糊
                    NSInteger interval = [calendar components:NSCalendarUnitDay fromDate:self toDate:today options:NSCalendarWrapComponents].day;
                    if (interval < 7) {
                        dateFmt.dateFormat = weekdayArray[myComponents.weekday-1];
                    }
                }
            }
            
        }
    }
    
    return [prefix stringByAppendingString:[dateFmt stringFromDate:self]];
}

- (NSString *)timeMessageString {
    
    NSDate *today = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    NSDateComponents *todayComponents = [calendar components:unit fromDate:today];
    
    NSDateComponents *myComponents = [calendar components:unit fromDate:self];
    
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc ] init ];

    NSString *prefix = @"";

    if (todayComponents.year != myComponents.year) {
        dateFmt.dateFormat = @"yyyy-MM-dd HH:mm";
    } else {

        // 当天（月份和日期一致才是）
        if (todayComponents.month == myComponents.month && todayComponents.day == myComponents.day) {
            dateFmt.dateFormat = @"HH:mm";
        } else {
            // 昨天（以“现在”的时候为基准-1天）
            NSDate *yesterdayDate = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:today];
            NSDateComponents *yesterdayComponents = [calendar components:unit fromDate:yesterdayDate];

            // 前天（以“现在”的时候为基准-2天）
            NSDate *beforeYesterdayDate = [NSDate dateWithTimeInterval:-48*60*60 sinceDate:today];
            NSDateComponents *beforeYesterdayComponents = [calendar components:unit fromDate:beforeYesterdayDate];

            if (yesterdayComponents.month == myComponents.month && yesterdayComponents.day == myComponents.day) {
                prefix = [GetHCLocalizableString(@"Yesterday") stringByAppendingString:@" "];
                dateFmt.dateFormat = @"HH:mm";
            } else if (beforeYesterdayComponents.month == myComponents.month && beforeYesterdayComponents.day == myComponents.day) {
                prefix = [GetHCLocalizableString(@"BeforeYesterday") stringByAppendingString:@" "];
                dateFmt.dateFormat = @"HH:mm";
            } else {
                dateFmt.dateFormat = @"MM-dd HH:mm";
            }
        }
    }
    
    return [prefix stringByAppendingString:[dateFmt stringFromDate:self]];
}

+ (NSString *)shortTimeMessageString:(NSString *)timestamp week:(BOOL)week {
    NSDate *date = [NSDate date:timestamp];
    return [date shortTimeMessageStringAtWeek:week];
}

+ (NSString *)shortTimeMessageString:(NSString *)timestamp {
    return [self shortTimeMessageString:timestamp week:NO];
}

+ (NSString *)timeMessageString:(NSString *)timestamp {
    NSDate *date = [NSDate date:timestamp];
    return [date timeMessageString];
}

@end
